<?php
/**
 * Created by PhpStorm.
 * User: 17628
 * Date: 2021-09-13
 * Time: 8:44
 */

namespace app\api\controller;

use app\admin\validate\UserTable as UserTableValidate;
use app\admin\model\UserTable;
use app\common\controller\Api;
use fast\Random;

class Site extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 注册会员
     * @ApiMethod (POST)
     * @param string $username 用户名
     * @param string $password 密码
     * @param string $pay_password 密码
     * @param string $mobile 手机号
     * @param string $invite_code 验证码
     */
    public function register()
    {
        //
        $data = $this->request->post();
        $validate = new UserTableValidate();
        if (!$validate->scene('register')->check($data)) {
            $this->error($validate->getError());
        }
        //todo 先 验证 验证码 对不对

        $data['salt'] = getRandStr(6);  //密码盐
        $data['pay_salt'] = getRandStr(6); //支付密码密码盐
        $data['password'] = makePassword($data['password'],$data['salt']);  //密码
        $data['pay_password'] = makePassword($data['pay_password'],$data['pay_salt']);//支付密码
        unset($data['repassword']);
        $p = UserTable::where('invite_code',$data['invite_code'])->find();
        $data = [
            'mobile' => $data['mobile'],
            'salt' => $data['salt'],
            'pay_salt' => $data['pay_salt'],
            'password' => $data['password'],
            'pay_password' => $data['pay_password'],
            'pid'=>$p['id'],
            'nickname' => $data['nickname'],
            'invite_code' => Random::num(),
        ];
        $res = UserTable::create($data);
        // halt($res);
        if ($res){
            $token = signToken($res->id);
            $res_user = ['mobile'=>$res->mobile,'id'=>$res->id];
            $this->success('注册成功',['token'=>$token,'user'=>$res_user]);

        }
        $this->error('注册失败！');

    }

    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @param string $mobile 账号
     * @param string $password 密码
     */
    public function login()
    {
        $mobile = $this->request->post('mobile');
        $password = $this->request->post('password');
        if (!$mobile || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $user =UserTable::where('mobile',$mobile)->find();
        if(!$user){
            $this->error('手机号未注册');
        }
        if($user->status_switch != 1){
            $this->error('该账号已被拉黑');
        }

        if(makePassword($password,$user->salt) != $user->password){
            $this->error('密码错误');
        }

        $token = signToken($user->id);
        $res_user = ['nickname'=>$user->nickname,'id'=>$user->id,'mobile'=>$user->mobile,];
        $this->success('登录成功',['token'=>$token,'user'=>$res_user]);
    }
    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

}